//
//  GMSkeletonView.m
//  gentleman
//
//  Created by 范月成 on 2017/12/18.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMSkeletonView.h"
#import <objc/runtime.h>

static void *kContainerKey = &kContainerKey;
static const CGFloat kShadowWidth = 60.;

@interface GMSkeletonView ()

@property (nonatomic, strong) CAGradientLayer *animationLayer;

@end

@implementation GMSkeletonView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    if (!self.bgColor) {
        self.bgColor = kDefaultBackgroundColor;//kColorRGB(200, 200, 200, 1)
    }
    
    self.backgroundColor = self.bgColor;
    self.layer.masksToBounds = YES;
    [self.layer addSublayer:self.animationLayer];
    [self animate];
}

- (void)animate {
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"position"];
    basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(-kShadowWidth/2., self.bounds.size.height/2.)];
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(self.bounds.size.width+kShadowWidth/2., self.bounds.size.height/2.)];
    basic.duration = 1.5;
    basic.repeatCount = INFINITY;
    [self.animationLayer addAnimation:basic forKey:basic.keyPath];
}

#pragma mark - getter
-(CAGradientLayer *)animationLayer {
    if (!_animationLayer) {
        _animationLayer = [CAGradientLayer layer];
        _animationLayer.frame = CGRectMake(0, 0, kShadowWidth, self.bounds.size.height);
        _animationLayer.colors = @[(id)[self.bgColor colorWithAlphaComponent:0.1].CGColor,
                              (id)[[UIColor whiteColor] colorWithAlphaComponent:0.25].CGColor,
                              (id)[[UIColor whiteColor] colorWithAlphaComponent:0.4].CGColor,
                              (id)[[UIColor whiteColor] colorWithAlphaComponent:0.25].CGColor,
                              (id)[self.bgColor colorWithAlphaComponent:0.1].CGColor];
        _animationLayer.startPoint = CGPointMake(0, 0.5);
        _animationLayer.endPoint = CGPointMake(1, 0.5);
        
    }
    return _animationLayer;
}

@end

@implementation UIView (GMSkeleton)

- (void)setContainer:(UIView *)container {
    container.frame = self.bounds;
    
    UIColor *color = kDefaultContainerBackgroundColor;
    if ([self respondsToSelector:@selector(skeletonBackgroundColor)]) {
        color = [(UIView<GMSkeletonLayoutProtocol> *)self skeletonBackgroundColor];
    }
    
    container.backgroundColor = color;
    
    [self addSubview:container];
    
    objc_setAssociatedObject(self, kContainerKey, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)container{
    return objc_getAssociatedObject(self, kContainerKey);
}


- (void)skeletonStartAnimation {
    if ([self conformsToProtocol:@protocol(GMSkeletonLayoutProtocol)] == NO) {
        return;
    }
    
    if ([self respondsToSelector:@selector(skeletonLayout)] == NO) {
        return;
    }
    
    self.userInteractionEnabled = NO;
    
    [self buildContainer];
    
    [self bringSubviewToFront:self.container];
    
    NSArray<GMSkeletonView *> *somoViews = [(UIView<GMSkeletonLayoutProtocol> *)self skeletonLayout];
    
    [self buildSkeletonSubViews:somoViews];
}

- (void)skeletonStopAnimation {
    if (!self.container) {
        return;
    }
    self.userInteractionEnabled = YES;
    [self clear];
}

- (void)buildContainer{
    [self clear];
    self.container = [UIView new];
}

- (void)buildSkeletonSubViews:(NSArray<GMSkeletonView *> *)views {
    for (GMSkeletonView * view in views) {
        [self.container addSubview:view];
    }
}

- (void)clear {
    [self.container removeFromSuperview];
    self.container = nil;
}

@end

@implementation GMSkeletonDataSourceProvider
{
    NSString * _reuseIdentifier;
}

- (instancetype)initWithCellReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super init]) {
        _reuseIdentifier = reuseIdentifier;
        _numberOfRowsInSection = 15;
    }
    return self;
}

+ (instancetype)dataSourceProviderWithCellReuseIdentifier:(NSString *)reuseIdentifier{
    return [[[self class] alloc] initWithCellReuseIdentifier:reuseIdentifier];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.numberOfRowsInSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_reuseIdentifier forIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [cell skeletonStartAnimation];
}

@end

